{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0348c8f0",
   "metadata": {},
   "source": [
    "requests库是基于urllib实现的更加方便使用的http请求库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38b19d74",
   "metadata": {},
   "source": [
    "pip install requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "824ff3a0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('http://www.qq.com', timeout=4)\n",
    "print(type(response))\n",
    "print(response.status_code)\n",
    "print(type(response.text))\n",
    "print(response.text)\n",
    "print(response.cookies)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7fc8a2c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "response = requests.get('http://httpbin.org/get')\n",
    "print(response.json())\n",
    "print(json.loads(response.text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "38d7cb61",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('https://github.com/favicon.ico')\n",
    "with open('.favicon.ico', 'wb') as f:\n",
    "    f.write(response.content)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bcae237",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "data = { 'name': 'germey', 'age': 22 }\n",
    "response = requests.post('http://httpbin.org/post', data=data)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d524a8f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3883.400 QQBrowser/10.8.4559.400'\n",
    "}\n",
    "response = requests.get('https://www.zhihu.com/explore', headers=headers)\n",
    "print(response.text)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04dbfe4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "data = {\n",
    "    'name': 'green'\n",
    "}\n",
    "response = requests.post('https://httpbin.org/post', data=data)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9527553e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "data = {\n",
    "    'name': 'green'\n",
    "}\n",
    "response = requests.post('https://httpbin.org/post', data=data)\n",
    "print(response.status_code)\n",
    "print(response.headers)\n",
    "print(response.cookies)\n",
    "print(response.url)\n",
    "print(response.history)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2f845cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.post('https://httpbin.org/post', data=data)\n",
    "\n",
    "if response.status_code == 200:\n",
    "    print('Request ok')\n",
    "else:\n",
    "    print('Request error')                                                  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "57cb5d26",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "files = {\n",
    "    'file': open('.favicon.ico', 'rb')\n",
    "}\n",
    "requests.post('http://httpbin.org/post', files=files)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abdf1ff8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "requests.get('https://www.baidu.com')\n",
    "print(response.cookies)\n",
    "for key, value in response.cookies.items():\n",
    "    print(key + ':' + value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84ff3cc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "s = requests.Session()\n",
    "s.get('http://httpbin.org/cookies/set/number/123456789')\n",
    "response = s.get('http://httpbin.org/cookies')\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a2c93f0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from requests.packages import urllib3\n",
    "urllib3.disable_warnings()\n",
    "response = requests.get('https://www.baidu.com', verify=False)\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d299ecd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('https://www.baidu.com', verify=False, cert=('path/server.cry', '/path/key'))\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a807e44b",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install 'requests[socks]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89cdc7ad",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "proxies = {\n",
    "    'http': 'http://user:passport@127.0.0.1:1456',\n",
    "    'https': 'http://user:passport@127.0.0.1:1456'\n",
    "}\n",
    "requests.get('http://httpbin.org/cookies/set/number/123456789', proxies=proxies)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "071152da",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "proxies = {\n",
    "    'http': 'socks5://127.0.0.1:1456',\n",
    "    'https': 'socks5://127.0.0.1:1456'\n",
    "}\n",
    "requests.get('http://httpbin.org/cookies/set/number/123456789', proxies=proxies)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2fa47392",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from requests.exceptions import ReadTimeout, ConnectionError, RequestException\n",
    "\n",
    "try:\n",
    "    requests.get('https://www.baidu.com')\n",
    "except ReadTimeout:\n",
    "    print('Timeout')\n",
    "except ConnectionError:\n",
    "    print('connect error')\n",
    "except RequestException:\n",
    "    print('Error')\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4e7b21ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "<class 'dict'>\n",
      "[{'koa.sid.sig': '8Ct2N3TBiZTgGCjpnYbDBoDCDGs'}, {'koa.sid': 'BkHLhI2ZjSbtPrksvFLSCJVZPbRXG5kK'}]\n"
     ]
    }
   ],
   "source": [
    "cookies = [{\n",
    "'domain': 'rap2.local',\n",
    "'expiry': 1650444225,\n",
    "'httpOnly': True,\n",
    "'name': 'koa.sid.sig',\n",
    "'path': '/',\n",
    "'secure': False,\n",
    "'value': '8Ct2N3TBiZTgGCjpnYbDBoDCDGs'\n",
    "}, {\n",
    "'domain': 'rap2.local',\n",
    "'expiry': 1650444225,\n",
    "'httpOnly': True,\n",
    "'name': 'koa.sid',\n",
    "'path': '/',\n",
    "'secure': False,\n",
    "'value': 'BkHLhI2ZjSbtPrksvFLSCJVZPbRXG5kK'\n",
    "}]\n",
    "requestsCookies = []\n",
    "\n",
    "for cookie in cookies:\n",
    "    dt = {}\n",
    "    dt[cookie['name']] = cookie['value']\n",
    "    requestsCookies.append(dt)\n",
    "    print(type(dt))\n",
    "print(requestsCookies)\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "07457678",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "<class 'dict'>\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "\n",
    "\n",
    "print(type([]))\n",
    "print(type({}))\n",
    "print(str(type([])) == \"<class 'list'>\")\n",
    "print(str(type({})) == \"<class 'dict'>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "686efb98",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
